package com.ledim.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.ChatRoomBean;
import com.ledim.bean.LedimChatMsgBean;
import com.ledim.ledimview.LedimUserIconView;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.DensityUtil;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LedimFunctionUtils;
import com.ledim.utils.LedimSpUtils;
import com.ledim.utils.LedimViewHolder;
import com.ledim.utils.LogUtil;
import com.ledim.utils.TimeUtil;
import com.letv.android.young.client.R;
import java.util.ArrayList;

/**
 *
 */
public class ChatAdapter extends LedimBaseListAdapter<LedimChatMsgBean> {

    private boolean mIsFull = false;
    private boolean mIsWuc = false;
    private ChatRoomBean mRoom;
    private final int Type_Full_ItemRight = 0;
    private final int Type_Full_ItemLeft = 1;
    private final int Type_Half_ItemRight = 2;
    private final int Type_Half_ItemLeft = 3;

    public ChatAdapter(Context context, ArrayList<LedimChatMsgBean> list, ChatRoomBean room,
                       boolean isWuc) {
        super(context);
        setDatas(list);
        this.mRoom = room;
        mIsWuc = isWuc;
    }

    public ChatAdapter(Context context, ArrayList<LedimChatMsgBean> list, boolean isFull,
                       ChatRoomBean room, boolean isWuc) {
        super(context);
        setDatas(list);
        this.mIsFull = isFull;
        this.mRoom = room;
        mIsWuc = isWuc;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LedimChatMsgBean chat = getItem(position);
        boolean isMe = false;
        if (LedimSpUtils.isLogin() && chat.uid != null) {
            isMe = LedimSpUtils.getUid().equals(chat.uid);
        }
        int type = getItemViewType(position);
        LedimViewHolder viewHolder = null;
        switch (type) {
            case Type_Full_ItemRight:
                viewHolder =
                        LedimViewHolder.get(context, convertView, R.layout.full_chat_item_right);
                break;
            case Type_Full_ItemLeft:
                viewHolder =
                        LedimViewHolder.get(context, convertView, R.layout.full_chat_item_left);
                break;
            case Type_Half_ItemRight:
                viewHolder =
                        LedimViewHolder.get(context, convertView, R.layout.half_chat_item_right);
                break;
            case Type_Half_ItemLeft:
                viewHolder =
                        LedimViewHolder.get(context, convertView, R.layout.half_chat_item_left);
                break;
        }
        if (viewHolder.bindObj == null) {
            viewHolder.bindObj = new ViewHolder(viewHolder);
        }
        initView(position, (ViewHolder) viewHolder.bindObj, isMe);

        return viewHolder.getConvertView();
    }

    @Override
    public int getItemViewType(int position) {
        LedimChatMsgBean chat = getItem(position);
        boolean isMe = LedimSpUtils.isLogin() && chat.uid != null &&
                LedimSpUtils.getUid().equals(chat.uid);
        if (mIsFull) {
            if (isMe) {
                return Type_Full_ItemRight;
            } else {
                return Type_Full_ItemLeft;
            }
        } else {
            if (isMe) {
                return Type_Half_ItemRight;
            } else {
                return Type_Half_ItemLeft;
            }
        }
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    private void initView(int position, ViewHolder holder, boolean isMe) {
        LedimChatMsgBean chat = getItem(position);
        //处理 ---时间显示
        if (position > 0 &&
                ((chat.addtime - getDatas().get(position - 1).addtime)) / (1000 * 60) < 5) {
            holder.mJoinTime.setVisibility(View.GONE);
        } else {
            if (chat.addtime == 0) {
                holder.mJoinTime.setVisibility(View.GONE);
            } else {
                holder.mJoinTime.setText(TimeUtil.getChatTime(chat.addtime));
                holder.mJoinTime.setVisibility(View.VISIBLE);
            }
        }

        String userName = "";
        boolean isRoomCreater = false;
        if (chat.uid != null && chat.user != null && chat.user.nickname != null) {
            userName = "<font color='#D271D4'>" + chat.user.nickname + "</font>";
            if (chat.uid.equals(mRoom.user.userId)) {
                // 房主
                isRoomCreater = true;
                userName = "<font color='#E55C7D'>" + chat.user.nickname + "</font>";
            } else {
                // 非房主
                if (chat.uid.length() > 0) {
                    LogUtil.e("ccx uid msg===" + chat.message);
                    LogUtil.e("ccx uid===" + chat.user.uid);

                    userName =
                            LedimFunctionUtils.getUserNameColorString(chat.user.uid, chat.user.nickname);
                }
            }
        }

        switch (chat.type) {
            case ConstantUtil.ChatType.join:
                holder.mJoinView.setVisibility(View.VISIBLE);
                holder.mChatTextView.setVisibility(View.GONE);
                holder.mJoinName.setText(Html.fromHtml(userName + "  闪亮登场啦~"));
                break;

            case ConstantUtil.ChatType.notice:
                userName =
                        LedimFunctionUtils.getUserNameColorString(chat.user.uid, chat.user.nickname);
                holder.mJoinView.setVisibility(View.VISIBLE);
                holder.mChatTextView.setVisibility(View.GONE);
                holder.mJoinName.setText(Html.fromHtml(userName + " " + chat.message));
                break;
            case ConstantUtil.ChatType.coin:
                holder.mJoinView.setVisibility(View.VISIBLE);
                holder.mChatTextView.setVisibility(View.GONE);
                Bitmap b =
                        BitmapFactory.decodeResource(context.getResources(), R.drawable.room_gold);
                b =
                        Bitmap.createScaledBitmap(b, DensityUtil.dip2px(context, 13), DensityUtil.dip2px(context, 13), true);
                ImageSpan imgSpan = new ImageSpan(context, b);
                SpannableString spanString = new SpannableString("icon");
                spanString.setSpan(imgSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.mJoinName.setText(Html.fromHtml(userName + "  打赏了" + chat.coin + "个金币"));
                holder.mJoinName.append(spanString);
                holder.mJoinName.append("~");
                break;

            case ConstantUtil.ChatType.text:

                holder.mImojView.setVisibility(View.GONE);
                holder.mTextChatBg.setVisibility(View.VISIBLE);
                holder.mJoinView.setVisibility(View.GONE);
                holder.mChatTextView.setVisibility(View.VISIBLE);
                if (isRoomCreater) {
                    holder.mName.setTextColor(context.getResources()
                                                     .getColor(R.color.custom_progressbar_above));
                } else {
                    holder.mName.setTextColor(context.getResources()
                                                     .getColor(R.color.text_color_level9));
                }
                if (chat.user != null && chat.user.icon != null) {
                    holder.mUserIcon.setChatIcon((FragmentActivity) context, chat.user.icon, chat.uid, mIsWuc, isMe, mRoom);

                }
                if (chat.user != null && chat.user.nickname != null) {
                    holder.mName.setText(chat.user.nickname);
                }
                holder.mText.setTextColor(context.getResources()
                                                 .getColor(R.color.text_color_level9));
                chat.message = chat.message.replaceAll("＜", "<");
                chat.message = chat.message.replaceAll("＞", ">");
                chat.message = chat.message.replaceAll("&gt;", ">");
                holder.mText.setText(chat.message);
                if (chat.uid != null) {
                    if (chat.uid.equals(mRoom.user.userId)) {
                        // 房主
                        holder.mText.setTextColor(context.getResources()
                                                         .getColor(R.color.text_color_level7));
                        if (isMe) {
                            holder.mTextChatBg.setBackgroundResource(R.drawable.chat_createrright_bg);
                        } else {
                            holder.mTextChatBg.setBackgroundResource(R.drawable.chat_createrleft_bg);
                        }
                    } else {
                        // 非房主
                        if (chat.uid.length() > 0) {
                            switch (Integer.valueOf(chat.uid) % 3) {
                                case 0:
                                    if (isMe) {
                                        holder.mTextChatBg.setBackgroundResource(R.drawable.chat_right_bg1);
                                    } else {
                                        holder.mTextChatBg.setBackgroundResource(R.drawable.chat_left_bg1);
                                    }

                                    break;
                                case 1:
                                    if (isMe) {
                                        holder.mTextChatBg.setBackgroundResource(R.drawable.chat_right_bg2);
                                    } else {
                                        holder.mTextChatBg.setBackgroundResource(R.drawable.chat_left_bg2);
                                    }
                                    break;
                                case 2:
                                    if (isMe) {
                                        holder.mTextChatBg.setBackgroundResource(R.drawable.chat_right_bg3);
                                    } else {
                                        holder.mTextChatBg.setBackgroundResource(R.drawable.chat_left_bg3);
                                    }
                                    break;
                                default:
                                    if (isMe) {
                                        holder.mTextChatBg.setBackgroundResource(R.drawable.chat_right_bg1);
                                    } else {
                                        holder.mTextChatBg.setBackgroundResource(R.drawable.chat_left_bg1);
                                    }
                                    break;
                            }
                        }
                    }
                } else {
                    if (isMe) {
                        holder.mTextChatBg.setBackgroundResource(R.drawable.chat_right_bg1);
                    } else {
                        holder.mTextChatBg.setBackgroundResource(R.drawable.chat_left_bg1);
                    }
                }

                break;

            case ConstantUtil.ChatType.icon:
                if (chat.user != null && chat.user.icon != null) {
                    holder.mUserIcon.setChatIcon((FragmentActivity) context, chat.user.icon, chat.uid, mIsWuc, isMe, mRoom);
                }
                holder.mImojView.setVisibility(View.VISIBLE);
                holder.mTextChatBg.setVisibility(View.GONE);
                holder.mJoinView.setVisibility(View.GONE);
                holder.mChatTextView.setVisibility(View.VISIBLE);
                if (isRoomCreater) {
                    holder.mName.setTextColor(context.getResources()
                                                     .getColor(R.color.custom_progressbar_above));
                } else {
                    holder.mName.setTextColor(context.getResources()
                                                     .getColor(R.color.text_color_level9));
                }
                if (chat.user != null && chat.user.nickname != null) {
                    holder.mName.setText(chat.user.nickname);
                }
                FrescoUtil.loadUrl(chat.message, holder.mImojView);
                //holder.mImojView.setImageURI(Uri.parse(chat.message));
                break;
        }
    }

    public static class ViewHolder {

        private LinearLayout mChatTextView;
        private TextView mName;
        private TextView mText;
        private TextView mJoinTime;
        private TextView mJoinName;
        private LinearLayout mJoinView;
        private LedimUserIconView mUserIcon;
        private LinearLayout mTextChatBg;
        private SimpleDraweeView mImojView;

        public ViewHolder(LedimViewHolder holder) {
            mJoinTime = holder.getView(R.id.chat_item_join_time);
            mJoinView = holder.getView(R.id.chat_item_join_view);
            mJoinName = holder.getView(R.id.chat_item_join_name);
            mChatTextView = holder.getView(R.id.chat_item_text_view);
            mName = holder.getView(R.id.chat_item_name);
            mText = holder.getView(R.id.chat_item_text);
            mUserIcon = holder.getView(R.id.chat_item_avatar);
            mTextChatBg = holder.getView(R.id.ll_chattext_bg);
            mImojView = holder.getView(R.id.iv_emoji);
        }
    }
}
